package ru.ifmo.ipimenov.ewf_svn.standalone.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.UserDao;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.impl.UserDaoHibernate;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.User;
import ru.ifmo.ipimenov.ewf_svn.standalone.service.UserService;
import ru.ifmo.ipimenov.ewf_svn.standalone.service.exceptions.UserAuthenticationFailedException;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class UserServiceImpl implements UserService {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(UserServiceImpl.class);

    private UserDao dao;

    public UserServiceImpl() {
        dao = new UserDaoHibernate();
    }

    public User load(String username, String password) throws UserAuthenticationFailedException {
        User u = dao.load(username);
        if (u.getPass().equals(password)){
            return u;
        }else {
            throw new UserAuthenticationFailedException("User " + username + " provided wrong credentials");
        }
    }

    public void save(User u) {
        dao.save(u);
    }
}
